#include "objectManager.h"



objectManager::objectManager() {

    listMAX = 1000;
    listCNT = 0;

    cntCrossTasks = 0;

    debug = 0;


    // pointer
    netManager = NULL;
    msgSystem = NULL;
    dataManager = NULL;


    // logfile
    logfile.open("test.txt", fstream::out);


    // config
    configDistanceToCalculate = 3500;
    configBigTasksSeconds = 30;            // every x seconds to big task


    // stuff that need to be after config
    cntBigTasks = configBigTasksSeconds;  // do one calc at the beginning

}

objectManager::~objectManager() {
    logfile.close();
}


float objectManager::getDistance(float sourceX, float sourceY, float targetX, float targetY) {

    //Normalise
    long X=0, Y=0;
    if( sourceX < targetX ) X = targetX + (sourceX*-1);
    else X = sourceX + (targetX*-1);
    if( sourceY < targetY ) Y = targetY + (sourceY*-1);
    else Y = sourceY + (targetY*-1);

    return hypotf(X,Y);
}


int objectManager::getCount(void) {
    return listCNT;
}


void objectManager::Init(void) {
    //return listCNT;
}


void objectManager::IntroduceMessage(message *pMsgSystem) {
    msgSystem = pMsgSystem;
    if(debug) msgSystem->Add("obj: msgSystem linked");
}

void objectManager::IntroduceNetManager(networkManager *pNetManager) {
    netManager = pNetManager;
    if(debug) msgSystem->Add("obj: netManager linked");
}

void objectManager::IntroduceSQL(mysql *pDataManager) {
    dataManager = pDataManager;
    if(debug) msgSystem->Add("net: sql linked");
}



long objectManager::AddObject(object pObject) {

    logfile << "------Add Object" << endl;
    logfile << "ID: " << pObject.id << endl;
    logfile << "Name: " << pObject.name << endl;
    logfile << "Type: " << pObject.type << endl;
    logfile << "Status: " << pObject.status << endl;
    logfile << "account_id: " << pObject.account_id << endl;
    logfile << "character_id: " << pObject.character_id << endl;
    logfile << "x: " << pObject.x << endl;
    logfile << "y: " << pObject.y << endl;
    logfile << endl;

    if(listCNT < listMAX) {
        list.push_back(pObject);
        listCNT++;
        return pObject.id;
    } else {
        if(debug) msgSystem->Add("obj: Add: max List reached!");
    }
    return -1;
}


void objectManager::crossLoop(bool isSecond) {

    int i=0;
    int e=0;
    cntCrossTasks = 0;
    bool doAlsoGlobalCalc = 0;


    // Execute only every x seconds
    if(isSecond) {
        cntBigTasks++;
        if(cntBigTasks >= configBigTasksSeconds) {
            cntBigTasks = 0;
            doAlsoGlobalCalc = 1;
        }
    }



    for(i=0;i<listCNT;i++) {
        e=0;

        // single internal tasks
        singleTask(i);


        for(e=0;e<listCNT;e++) {



            // corresponding tasks
            if(i!=e) crossTask(i, e, isSecond, doAlsoGlobalCalc);



        }
    }
}




void objectManager::crossTask(int source, int target, bool isSecond, bool doGlobal) {

    // Check what to do from each view to each others,
    // MOST IMPORTANT SPEED RELEVANT
    // TODO: filter people who are to far away and do groups



    // check the distance between targets n to n
    if(doGlobal) {

        if(configDistanceToCalculate > getDistance(list[source].x,list[source].y,list[target].x,list[target].y)) list[source].targetInRange[target] = true;
        else list[source].targetInRange[target] = false;

    }


    if(list[source].targetInRange[target]) {


        if( list[source].status == 1 ) { // 1= Normal



            cntCrossTasks++;  // only if something was calculated




            // do second interval calculations
            if(isSecond) {

                // IF source is Human & ACTIVE then

                // IF distance is relevant add to watch list of PLAYER




            } // second

        } // status

    } // targetInRange

}




void objectManager::singleTask(int source) {

    // Check what to do from each view to each others,
    // MOST IMPORTANT SPEED RELEVANT


   // if( list[source].status == 1 ) {

        //logfile << "source(" << source << ") --> target(" << target <<")." << endl;
/*
        logfile << "--->source: " << source << endl;
        logfile << "source.player_id: " << list[source].player_id << endl;
        logfile << "source.character_id: " << list[source].character_id << endl;
        logfile << "source.name: " << list[source].name << endl;
        logfile << "source.x: " << list[source].x << endl;
        logfile << "source.y: " << list[source].y << endl;
        logfile << "source.id: " << list[source].id << endl;
        logfile << "source.typ: " << list[source].typ << endl;
        logfile << "source.status: " << list[source].status << endl << endl;
*/

    //}

    // Do the Network relevant Client Tasks
    if( list[source].controlledBy > 0) {    // HUMAN

        //

    } else if( list[source].controlledBy = 0) { // NPC

        //
    }
    // Rest is SLEEPING


}




int objectManager::getCrossTasks(void) {
    return cntCrossTasks;
}






